# -*- coding: utf-8 -*-
# @Time : 2022/5/26 9:17
# @Author : WangYunfei
# @FileName: demo1.py
# @Email: yunfei.wang@innvote.com
import requests


def get_image_content(urls):
    """
    获取图片的二进制数据
    :param urls: 图片链接列表
    :return: 返回 (图片二进制数据, 图片名称)
    """
    for url in urls:
        req = requests.get(url)
        if req.status_code == 200:
            yield req.content, url.rsplit("/")[-1]
        else:
            print(f"获取图片数据失败：图片链接为：{url}")
    return None, None


def save_images(urls):
    """
    保存图片
    :param urls: 图片链接列表
    :return:
    """
    for content, image_name in get_image_content(urls):
        with open(image_name, "wb") as img:
            img.write(content)


if __name__ == '__main__':
    url_list = [
        "https://pic.netbian.com/uploads/allimg/220211/004115-1644511275bc26.jpg",
        "https://pic.netbian.com/uploads/allimg/220215/233510-16449393101c46.jpg",
        "https://pic.netbian.com/uploads/allimg/211120/005250-1637340770807b.jpg",
    ]
    save_images(url_list)




